<?php

namespace App\Http\Controllers\Wechat;

use App\Http\Controllers\Controller;
use App\Http\Requests\DoctorAuth;
use App\Models\Department;
use App\Models\Doctor;
use Illuminate\Http\Request;


class DoctorController extends Controller
{
    // 医师列表
    public function index(Request $request)
    {
        $department_id = $request->post('department_id');
        $columns = [
            'department_id', 'department_name', 'avatar',
            'name', 'positions', 'hospital',
            'price', 'visits_num', 'skilled',
            'id'
        ];

        if ($department_id) {
            $result = Doctor::whereDepartmentId($department_id)
                ->paginate($this->limit, $columns);
        } else {
            $result = Doctor::paginate($this->limit, $columns);
        }

        return $this->writeJson(0, $result->items());
    }

    // 医师认证
    public function auth(DoctorAuth $request)
    {
        $model = Doctor::whereUserId(auth('wechat')->id())
            ->first();
        if (empty($model)) {
            $model = new Doctor();
            $model->user_id = auth('wechat')->id();
        }
        $model->status = 0; // 修改认证资料需要重新审核
        $department_id = $request->post('department_id');
        $model->department_id = $department_id;
        $model->department_name = Department::whereId($department_id)->value('name');
        $model->name = $request->post('name');
        $model->skilled = $request->post('skilled');
        $model->phone = $request->post('phone');
        $model->id_num = $request->post('id_num');
        $model->cert_img = $request->post('cert_img');

        $model->save();
        return $this->writeJson(0, null, '提交成功');
    }
}
